#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
@FileName  :serializers.py 
@desc      :
@Author    :Young
@IDE       :PyCharm
@Time      :2021/8/14 0014 12:03
'''
from rest_framework.serializers import ModelSerializer
from rest_framework import serializers

from Admin.adminSerializers import ArticleThemeSerializer, UserSerializer, QuestionThemeSerializer
from Admin.models import Article_comment,Articles,Q_Answer,Q_answer_comment, Questions,\
     User
from Web1.models import Book_recommend, Book_category, Feedback, Movie, Wish, FamilyTroubles,FamilyTroublesResponse,\
    StudyPressure, StudyPressureResponse, WorkPressure,WorkPressureResponse, EmotionsTroubles,EmotionsTroublesResponse,\
    JieYou, PsychologicalConsultant, PsychologicalConsultantCategory


class RegisterSerializer(serializers.Serializer):
    #需要验证的信息
    username = serializers.CharField(max_length=50, required=True)
    password = serializers.CharField(max_length=50, required=True)
    phone = serializers.CharField(max_length=20, required=True)
    role = serializers.CharField(max_length=10, required=True)

    def validate(self, attrs):
        #获取传递过来的信息，然后进行验证，该用户已经注册过，就返回错误信息，新注册用户就通过验证
        #1.获取注册用户的手机号
        registerPhone = attrs.get('phone')
        #2.判断该用户是否被注册过
        user = User.objects.filter(phone=registerPhone)
        if user.exists(): #该用户存在，抛出异常
            raise serializers.ValidationError('该用户已注册')
        return attrs

class UserLoginSerializer(serializers.Serializer):
    phone = serializers.CharField(max_length=11, required=True)
    password = serializers.CharField(max_length=50, required=True)

    def validate(self, attrs):
        #获取传递过来的用户手机号和密码
        account = attrs.get('phone')
        user_password = attrs.get('password')
        #查询该用户是否在数据库中
        user = User.objects.filter(phone=account)
        if not user.exists(): #该用户不存在，抛出异常
            raise serializers.ValidationError({'account':"该用户不存在"})
        #该用户存在
        user = user.first()
        #验证密码是否正确
        if user_password != user.password: #密码不正确，抛出异常
            raise serializers.ValidationError({'invalid':"用户名或密码不正确"})
        return attrs

class updateUserInfoSerializer(serializers.Serializer):
    username = serializers.CharField(max_length=11, required=True)



class RecommendArticleSerializer(ModelSerializer):
    article_theme_id = ArticleThemeSerializer()
    article_author_id = UserSerializer()
    class Meta:
        model = Articles
        fields = "__all__"


class LatestQuestionsSerializer(ModelSerializer):
    q_theme_id = QuestionThemeSerializer()
    class Meta:
        model = Questions
        fields = "__all__"

#文章评论信息的序列化器
class ArticleCommentsSerializer(ModelSerializer):
    # aid = RecommendArticleSerializer()
    uid = UserSerializer()
    class Meta:
        model = Article_comment
        fields = "__all__"

#问题回答的序列化器
class QuestionAnswerSerializer(ModelSerializer):
    # qid = LatestQuestionsSerializer()
    answer_user_id = UserSerializer()

    class Meta:
        model = Q_Answer
        fields = "__all__"
#问题回答评论的序列化器
class QuestionAnswerCommentSerializer(ModelSerializer):
    answer_id = QuestionAnswerSerializer()
    uid = UserSerializer()

    class Meta:
        model = Q_answer_comment
        fields = "__all__"

# 书籍分类
class BookCategorySerializer(ModelSerializer):

    class Meta:
        model = Book_category
        fields = "__all__"

# 书籍推荐序列化器
class RecommendBookSerializer(ModelSerializer):
    class Meta:
        model = Book_recommend
        fields = "__all__"

# 电影相关的序列化器
class MoviesSerializer(ModelSerializer):
    class Meta:
        model = Movie
        fields = "__all__"
# 反馈的序列化器
class FeedbackSerializer(serializers.Serializer):
    opinion_content = serializers.CharField(max_length=1000, allow_blank=False)
    #.TextField(max_length=1000, blank=True, null=False)

# wishes 相关序列化器
class WishSerializer(ModelSerializer):
    # wish_content = serializers.CharField(max_length=600)
    # uid = serializers.IntegerField()
    class Meta:
        model = Wish
        fields = "__all__"
# family_troubles
class FamilyTroublesSerializer(ModelSerializer):
    class Meta:
        model = FamilyTroubles
        fields = "__all__"
# family_troubles_response
class FamilyTroublesResponseSerializer(ModelSerializer):
    class Meta:
        model = FamilyTroublesResponse
        fields = "__all__"

# emotion_troubles
class EmotionsTroublesSerializer(ModelSerializer):
    class Meta:
        model = EmotionsTroubles
        fields = "__all__"
# emotion_troubles
class EmotionsTroublesResponseSerializer(ModelSerializer):
    class Meta:
        model = EmotionsTroublesResponse
        fields = "__all__"
# study_pressure
class StudyPressureSerializer(ModelSerializer):
    class Meta:
        model = StudyPressure
        fields = "__all__"
# study_pressure
class StudyPressureResponseSerializer(ModelSerializer):
    class Meta:
        model = StudyPressureResponse
        fields = "__all__"

# work_pressure
class WorkPressureSerializer(ModelSerializer):
    class Meta:
        model = WorkPressure
        fields = "__all__"
# work_pressure
class WorkPressureResponseSerializer(ModelSerializer):
    class Meta:
        model = WorkPressureResponse
        fields = "__all__"
#jieyou
class JieyouSerializer(ModelSerializer):
    class Meta:
        model = JieYou
        fields = "__all__"

# 心理咨询师相关
class PsychologicalConsultantCategorySerializer(ModelSerializer):
    class Meta:
        model =PsychologicalConsultantCategory
        fields = "__all__"

class PsychologicalConsultantSerializer(ModelSerializer):
    class Meta:
        model = PsychologicalConsultant
        fields = "__all__"